<?php

class Posts_model extends CI_Model{

	function getPosts( $limit=''){
		$limit_txt = (empty($limit))? '':'LIMIT '.$limit;
		$q_str = "
				  SELECT a.message,a.userid,b.picture,a.date_added as date_added FROM mlme_posts a, mlme_users b WHERE a.status = 1 AND a.userid = b.userid
				  UNION ALL 
				  SELECT c.description as message,c.userid,b.picture,c.date_added as date_added FROM mlme_messages c, mlme_users b WHERE c.type = 'notes' AND c.totype = 'public' AND c.userid = b.userid AND b.notified_by LIKE '%,%'	
				  ORDER BY date_added DESC $limit_txt
				  ";
		$query = $this->db->query($q_str);
		return $query;
	}
	function get_userid($email){
		 $query = $this->db->query("SELECT userid FROM mlme_users WHERE email ='".$email."'");
		 $result = $query->row();
		 return $result->userid;
	}
	//Make sure all columns from both tables were sychronized and in the same order
	function get_remembrance_wall($start_row='',$limit = ''){
		//echo 'start=>'.$start_row.' limit=>'.$limit;
		$limit_txt = ($start_row == '' && $limit == '')? '':'LIMIT '.$start_row.','.$limit;
		$q_str = "
				  SELECT a.postid,a.message,a.userid,a.post_to,a.files,b.picture,a.date_added as date_added FROM mlme_posts a LEFT JOIN mlme_users b ON a.userid = b.userid WHERE a.status = 1 
				  UNION ALL 
				  SELECT c.messageid as postid,c.description as message,c.userid,c.totype as post_to,c.files,b.picture,c.date_added as date_added FROM mlme_messages c LEFT JOIN mlme_users b ON c.userid = b.userid WHERE c.type = 'notes' AND c.totype = 'public' 	
				  ORDER BY date_added DESC $limit_txt
				  ";
		$query = $this->db->query($q_str);
		return $query->result_array();
	}
	function save_remembrance_wall($post,$c_date,$birthday,$date_of_death){
		$image = explode('/',$post['file_name']);
		$image = strtolower($image[count($image)-1]);
		$data = array(
			 'userid'   => $this->get_userid($_SESSION['email']),
			 'message'  => $post['add-remwall-msg'],
			 'post_to'  => $post['add-remwall-name'],
			 'birthday' => $birthday,
		'date_of_death' => $date_of_death,
			 'files'     => $image,
		   'date_added' => $c_date,
		 'date_updated' => $c_date,
			   'status' => 1
		);
		$insert = $this->db->insert('mlme_posts',$data);
		return $insert;

	}
	function get_post_birth_death_date($postid){
		$query = $this->db->query("SELECT birthday,date_of_death FROM mlme_posts WHERE postid=".$postid);
		return $query->row_array();
	}//end fct
}